package com.excel;

import com.baomidou.mybatisplus.core.conditions.*;
import com.demo.base.*;
import com.demo.model.*;
import com.github.liaochong.myexcel.core.*;
import com.github.liaochong.myexcel.utils.*;
import com.util.*;
import lombok.extern.slf4j.*;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.core.io.*;
import org.springframework.http.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.*;
import org.springframework.web.multipart.*;

import javax.servlet.http.*;
import java.lang.*;
import java.util.*;

/**
 * AIKA
 * 2024/6/20
 */

@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ExcelService {

    public final static String xlsx = ".xlsx";

    @Autowired
    private MyFactory myFactory;

    public BaseFactory factory() {
        return myFactory.factory;
    }

    private String fileName(String name) {
        return name + Generator.randomStr(100000) + xlsx;
    }


    public void myExcelExport(List list, Class clazz, String name) throws Exception {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Workbook workbook = DefaultExcelBuilder.of(clazz)
                .style("title->text-align:center", "vertical-align:center")
                .rowHeight(20).build(list);
        AttachmentExportUtil.export(workbook, name, response);
    }

   /* public void easyExport(List list, Class clazz, String name) {
        HttpServletResponse res = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        ExcelUtil util = new ExcelUtil(clazz);
        util.exportExcel(res, list, name);
    }*/

    public ResponseEntity<Resource> download() {
        try {
            String path = "template/model.xlsx";
            Resource resource = new ClassPathResource(path);
            IfUtil.If(!resource.exists()).Throw("excel文件不存在");
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=company.xlsx");
            headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(resource.contentLength()));
            return ResponseEntity.ok().headers(headers).body(resource);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new MyException("下载失败");
        }
    }

    public String readCompany(@RequestParam("multipartFile") MultipartFile file) throws Exception {
        List<Model> list = null;
        try {
            list = DefaultExcelReader.of(Model.class).sheet(0)
                    .rowFilter(row -> row.getRowNum() > 0).read(file.getInputStream());
            list.forEach(System.out::println);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new MyException("导入失败,请检查数据");
        }

        factory().modelRepository.saveAll(list);
        String msg = "成功导入" + list.size() + "条数据";
        return msg;
    }


    public void openExcel(Wrapper wrapper) throws Exception {
        List<Model> list = factory().modelService.list(wrapper);
        myExcelExport(list, Model.class, fileName("Model"));
    }


}
